package com.doradance.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Description:
 * Created by yong on 11/29/2017.
 */

public abstract class BaseFragment extends Fragment {
    public static final String TAG = "Fragment Lifecycle";
    private View rootView;
    private boolean isResume;
    private boolean isFirstToUser = false;
    boolean isDebug = false;

    @Override
    public void onAttach(Context context) {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onAttach ");
        }
        super.onAttach(context);


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onCreate ");
        }
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onCreateView ");
        }
        if (getFragmentLayout() < 1) {
            throw new IllegalStateException("fragment content ID not use");
        }
        rootView = inflater.inflate(getFragmentLayout(), container, false);
        bindView();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onActivityCreated ");
        }
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onStart ");
        }
        super.onStart();
    }

    @LayoutRes
    protected abstract int getFragmentLayout();


    protected void bindView() {
    }

    @Override
    public void onResume() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onResume");
        }
        super.onResume();
        isResume = true;
        if (getUserVisibleHint()) {
            if (!isFirstToUser) {
                onFirstUserVisible();
                isFirstToUser = true;
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " setUserVisibleHint: " + isVisibleToUser);
        }
        super.setUserVisibleHint(isVisibleToUser);
        if (isResume && isVisibleToUser) {
            if (!isFirstToUser) {
                onFirstUserVisible();
                isFirstToUser = true;
            } else {
                onUserVisible();
            }
        }
    }

    @Override
    public void onPause() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onPause");
        }
        super.onPause();
    }

    @Override
    public void onStop() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onStop");
        }
        super.onStop();
    }

    protected void onFirstUserVisible() {

    }

    protected void onUserVisible() {

    }


    protected View findViewById(@IdRes int id) {
        if (rootView == null) {
            throw new IllegalStateException("rootView  destroyed");
        }
        return rootView.findViewById(id);
    }

    @Override
    public void onDestroyView() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onDestroyView");
        }
        super.onDestroyView();
        rootView = null;
    }

    @Override
    public void onDetach() {
        if (isDebug) {
            Log.d(TAG, getClass().getName() + " onDetach");
        }
        super.onDetach();
        isResume = false;
        isFirstToUser = false;
    }
}
